package com.rytx.web.controller.ysp;

import com.rytx.common.core.controller.BaseController;
import com.rytx.common.core.domain.AjaxResult;
import com.rytx.common.core.domain.model.LoginUser;
import com.rytx.system.domain.ysp.ArtCollection;
import com.rytx.system.service.ArtCollectionService;
import com.rytx.system.service.IArtRentalService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@RestController
@RequestMapping("/artCollections")
@Api(value = "收藏管理", tags = {"收藏管理"})
public class ArtCollectionController extends BaseController {
    @Autowired
    private ArtCollectionService artCollectionService;

    @Autowired
    private IArtRentalService artRentalService;

    @GetMapping
    @ApiOperation(value = "分页查询收藏列表", notes = "分页查询收藏列表，传递分页参数")
    public AjaxResult getAllArtCollections() {
        LoginUser loginUser = getLoginUser();
        Long userid = loginUser.getArtUser().getId();
        startPage();
        List<ArtCollection> artCollections = artCollectionService.getArtCollectionsByUserId(userid);
        List<Map<String,Object>> listMap = new ArrayList<>();
        for (ArtCollection artCollection : artCollections) {
            Map<String,Object> map = new HashMap<>();
            map.put("collection",artCollection);
            map.put("rental",artRentalService.selectArtRentalById(artCollection.getArtRentalId()));
            listMap.add(map);
        }
        return AjaxResult.success(getDataTable(listMap));
    }

    @GetMapping("/{id}")
//    @ApiOperation(value = "Get art collection by ID", notes = "Retrieve a specific art collection by its ID")
    public AjaxResult getArtCollectionById(@PathVariable Long id) {
        return AjaxResult.success(artCollectionService.getArtCollectionById(id));
    }

    @PostMapping
    @ApiOperation(value = "新增收藏", notes = "新增收藏")
    public AjaxResult createArtCollection(@RequestBody ArtCollection artCollection) {
        LoginUser loginUser = getLoginUser();
        Long userid = loginUser.getArtUser().getId();
        artCollection.setUserId(String.valueOf(userid));
        artRentalService.addFavorite(artCollection.getArtRentalId(), 1l);
        return AjaxResult.success(artCollectionService.createArtCollection(artCollection));
    }

    @PutMapping("/{id}")
//    @ApiOperation(value = "Update an existing art collection", notes = "Update an existing art collection by its ID")
    public AjaxResult updateArtCollection(@PathVariable Long id, @RequestBody ArtCollection artCollection) {
        return AjaxResult.success(artCollectionService.updateArtCollection(id, artCollection));
    }

    @DeleteMapping("/{ids}")
    @ApiOperation(value = "删除收藏", notes = "删除收藏")
    public AjaxResult deleteArtCollection(@PathVariable Long ids) {
        Arrays.asList(ids).forEach(id -> {
            ArtCollection artCollection = artCollectionService.getArtCollectionById(id);
            artRentalService.addFavorite(artCollection.getArtRentalId(), -1l);
        });
        return AjaxResult.success(artCollectionService.deleteArtCollectionByIds(Arrays.asList(ids)));
    }
}
